package com.flyme.module.base.provider.apiPc;

import cn.hutool.core.date.DateUtil;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.security.OpenHelper;
import com.flyme.common.core.security.OpenUserAuth;
import com.flyme.common.core.utils.ApiAssert;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.entity.*;
import com.flyme.module.base.provider.service.*;
import com.flyme.module.exam.client.entity.BusAbility;
import com.flyme.module.exam.provider.service.BusAbiltityService;
import com.flyme.module.file.client.entity.SysCourse;
import com.flyme.module.file.client.entity.SysLive;
import com.flyme.module.file.client.entity.SysOffCourse;
import com.flyme.module.file.provider.service.SysCourseEnrollService;
import com.flyme.module.file.provider.service.SysCourseService;
import com.flyme.module.file.provider.service.SysLiveService;
import com.flyme.module.file.provider.service.SysOffCourseService;
import com.flyme.module.order.client.entity.Order;
import com.flyme.module.order.client.enums.OrderStatusEnum;
import com.flyme.module.order.provider.service.OrderService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;


/**
 * @author flyme
 */
@Slf4j
@Api(tags = "pc订单管理")
@RestController
@RequestMapping("/pcOrder")
public class PcApiOrderController extends BaseController<OrderService, Order> {

    @Autowired
    private SysInvoiceService invoiceService;
    @Autowired
    private SysBalanceService balanceService;
    @Autowired
    private SysCourseService courseService;
    @Autowired
    private SysLecturerService lecturerService;
    @Autowired
    private BusAbiltityService abilityService;
    @Autowired
    private SysLiveService liveService;
    @Autowired
    private SysChargeconfigService chargeconfigService;
    @Autowired
    private SysVipTypeService vipTypeService;
    @Autowired
    private SysOffCourseService offcourseService;
    @Autowired
    private SysBalancelogService balancelogService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private SysVipService vipService;
    @Autowired
    private SysCourseEnrollService courseenrollService;
    @Autowired
    private BusAbiltityService busAbilityService;
    @Autowired
    private SysBuyrecordService buyrecordService;
    @Autowired
    private SysConfigService sysSysconfigService;
    @Autowired
    private SysIntegralrecordService integralrecordService;
    @Autowired
    private SysBalanceService sysBalanceService;
    @Autowired
    private SysVipPriceService vipPriceService;

    /**yes
     * 取消订单
     */
    @RequestMapping(value = "/cancelOrderWeb")
    public ResultBody cancelOrderWeb(String orderId){
        ApiAssert.notNull("参数不能为空", orderId);
        Order order = bizService.getById(orderId);
        order.setCloseDate(DateUtil.now());
        order.setOrderStatus(OrderStatusEnum.close);
        boolean a = bizService.saveOrUpdate(order);
        if(a){
            return ResultBody.ok("订单取消成功");
        }
        return ResultBody.ok("订单取消失败");
    }

    /**yes
     * 根据用户查询所有的订单
     */
    @PostMapping(value = "/orderListWeb")
    public ResultBody orderListWeb(String orderStatus, PagerInfo pagerInfo){
        OpenUserAuth openUserAuth = OpenHelper.getUserAuth();
        EntityMap data = new EntityMap();
        PqGrid pq = bizService.selectByAccount2Web(openUserAuth.getUserId(), orderStatus, pagerInfo);
        data.put("list", pq);
        return ResultBody.success(data);
    }

    /**yes
     * 支付订单状态查询
     */
    @PostMapping(value = "/payorderStatusWeb")
    public ResultBody payorderStatusWeb(String orderId){
        ApiAssert.notNull("参数不能为空", orderId);
        EntityMap map = new EntityMap();
        Order order = bizService.getById(orderId);
        map.put("orderId", order.getId());
        map.put("orderStatus", order.getOrderStatus());
        map.put("orderAmount", order.getOrderAmount());
        map.put("payType", order.getPayType());
        return ResultBody.success(map);
    }

    /**
     * 根据目标id查询订单
     */
    @PostMapping(value = "/getOrderByTarget")
    public ResultBody getOrderByTarget(String targetId){
        ApiAssert.notNull("参数不能为空", targetId);
        EntityMap map = new EntityMap();
        Order order = bizService.getOne(q().eq("targetId", targetId), false);
        map.put("order", order);
        return ResultBody.success(map);
    }

    /**yes
     * 我的发票记录
     */
    @PostMapping(value = "/myInvoicePageWeb")
    public ResultBody myInvoicePageWeb( PagerInfo pagerInfo){
        OpenUserAuth openUserAuth = OpenHelper.getUserAuth();
        EntityMap map = new EntityMap();
        PqGrid pq = invoiceService.selectByUserWeb(openUserAuth.getUserId().toString(), pagerInfo);
        map.put("list", pq);
        return ResultBody.success(map);
    }

    /**
     * 用户待开票订单
     */
    @PostMapping(value = "/invoiceWaitWeb")
    public ResultBody invoiceWaitWeb( PagerInfo pagerInfo){
        OpenUserAuth openUserAuth = OpenHelper.getUserAuth();
        return orderService.selectOrders(pagerInfo, openUserAuth.getUserId());
    }


    /**yes
     * 订单返回支付页面
     * type：1购买课程2购买人才测评3直播报名4充值5vip购买6线下报名
     * applyType：1安卓2ios
     */
    @PostMapping(value = "/orderbuyPageWeb")
    public ResultBody orderbuyPageWeb(String orderId){
        EntityMap map = new EntityMap();
        OpenUserAuth openUserAuth = OpenHelper.getUserAuth();
        SysBalance balance = balanceService.findByUser(openUserAuth.getUserId());
        map.put("userAmount", balance.getAmount());
        Order order = bizService.getById(orderId);
//        BigDecimal targetAmount = new BigDecimal("0");
        BigDecimal targetAmount = order.getOrderAmount();
        map.put("needRecharge", 2);//不需要充值
        map.put("shortAmount", 0);
        if(targetAmount.compareTo(balance.getAmount()) > 0){
            map.put("needRecharge",1);//需要充值
            map.put("shortAmount",targetAmount.subtract(balance.getAmount()));
        }
        map.put("order", order);
        if(order.getOrderEntity().code.equals("OT_COURSE")){//课程购买
            SysCourse course = courseService.getById(order.getTargetId());
            SysLecturer lecturer = lecturerService.getById(course.getLecturerId());
            map.put("content", "讲师："+lecturer.getLecturerName());
        }
        if(order.getOrderEntity().code.equals("OT_APPRAISAL")){//人才测评购买
            BusAbility ability = abilityService.getById(order.getTargetId());
            map.put("content", "购买起"+ability.getExpireTime()+"天内有效");
        }
        if(order.getOrderEntity().code.equals("OT_LIVE")){//直播购买
            SysLive live = liveService.getById(order.getTargetId());
            map.put("content", "直播时间："+live.getLiveTime());
        }
        if(order.getOrderEntity().code.equals("OT_TOP")){//充值
            SysChargeconfig chargeconfig = chargeconfigService.getById(order.getTargetId());
            map.put("content", "学习币充值"+ chargeconfig.getDiscount());
        }
        if(order.getOrderEntity().code.equals("OT_VIP")){//vip
//            SysVipType vipType =vipTypeService.getById(order.getTargetId());
//            map.put("content", "购买起"+vipType.getVipTime()+"天内有效");
            SysVipPrice vipPrice = vipPriceService.getById(order.getTargetId());
            SysVipType vip = vipTypeService.getById(vipPrice.getVipTypeId());
            targetAmount = vipPrice.getAndroidPrice();
            map.put("content", "购买起" + vipPrice.getVipTime() + "月内有效");
            map.put("price", targetAmount);
        }
        if(order.getOrderEntity().code.equals("OT_OFFCOURSE")){//线下课程
            SysOffCourse offcourse = offcourseService.getById(order.getTargetId());
            SysLecturer lecturer = lecturerService.getById(offcourse.getLecturerId());
            map.put("content", "讲师："+lecturer.getLecturerName());
        }
        return ResultBody.success(map);
    }

    /**yes
     * PC端使用学习币支付订单
     *
     *            支付订单Id
     * @return 支付预订单信息
     */
    @PostMapping(value = "/balancePayWeb")
    public ResultBody balancePayWeb(String orderId) {
        return sysBalanceService.payBack(orderId, "Balance");
    }
}
